﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace _180FrameWork.CustControls
{
    public class CustDataGridViewFuzzyQueryBoxCell : DataGridViewTextBoxCell
    {
        public override void DetachEditingControl()
        {
            CustDataGridViewFuzzyQueryBoxEditingControl dataWindowControl = DataGridView.EditingControl as CustDataGridViewFuzzyQueryBoxEditingControl;
            dataWindowControl.AfterSelector -= new AfterSelectorEventHandler(dataWindowControl_AfterSelector);

            dataWindowControl.QueryStringChangeEvent -= new QueryStringChangeEventHandler(dataWindowControl_QueryStringChangeEvent);
            base.DetachEditingControl();

        }



        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            CustDataGridViewFuzzyQueryBoxEditingControl dataWindowControl = DataGridView.EditingControl as CustDataGridViewFuzzyQueryBoxEditingControl;
            //dataWindowControl.PopupGridAutoSize = true;
            CustDataGridViewFuzzyQueryBoxColumn dataWindowColumn = (CustDataGridViewFuzzyQueryBoxColumn)OwningColumn;

            dataWindowControl.DisplayMember = dataWindowColumn.SDisplayMember;//以下3句必须放在datasource设置前面
            dataWindowControl.ValueMember = dataWindowColumn.SDisplayField;

            //dataWindowControl.DataSource = dataWindowColumn.DataSource;//数据源变更通知

            //dataWindowControl.sKeyWords = dataWindowColumn.SKeyWords;
            dataWindowControl.Text = (string)this.Value;
            //dataWindowControl.RowFilterVisible = true;  //此句必须放在datasource设置后面

            //由于一个DataGridView中可能有多列输入法列，所以需要先移出每一列的所有事件的处里程序
            //foreach (DataGridViewColumn c in this.DataGridView.Columns)
            //{
            //    if (c.GetType() == typeof(CustDataGridViewFuzzyQueryBoxColumn))
            //    {
            //        CustDataGridViewFuzzyQueryBoxColumn col = (CustDataGridViewFuzzyQueryBoxColumn)c;
            //        col.AfterSelector -= (AfterSelectorEventHandler)(col_AfterSelector);
            //        col.QueryStringChangeEvent -= (Action<string>)(col_QueryStringChangeEvent);
            //    }
            //}
            //     For Each c As DataGridViewColumn In cell.DataGridView.Columns
            //    If TypeOf c Is DataGridViewPriceShortCutColumn Then
            //        Dim colShortCut As DataGridViewPriceShortCutColumn = CType(c, DataGridViewPriceShortCutColumn)
            //        RemoveHandler ctl.DictionaryReadOnlyChanged, AddressOf colShortCut.RaiseDictionaryReadOnlyChanged
            //        RemoveHandler ctl.DictionaryTextChanged, AddressOf colShortCut.RaiseDictionaryTextChanged
            //        RemoveHandler ctl.LoadDictFailed, AddressOf colShortCut.RaiseLoadDictFailed
            //        RemoveHandler ctl.BeforeDropDown, AddressOf colShortCut.RaiseBeforeDropDown
            //        RemoveHandler ctl.DictItemSelected, AddressOf colShortCut.RaiseDictItemSelected
            //    End If
            //Next


            dataWindowControl.AfterSelector += new AfterSelectorEventHandler(dataWindowControl_AfterSelector);

            dataWindowControl.QueryStringChangeEvent += new QueryStringChangeEventHandler(dataWindowControl_QueryStringChangeEvent);
        }

        void dataWindowControl_QueryStringChangeEvent(object sender, QueryStringChangeEventArgs e)
        {
            CustDataGridViewFuzzyQueryBoxColumn dataWindowColumn = (CustDataGridViewFuzzyQueryBoxColumn)OwningColumn;
            dataWindowColumn.OnQueryStringChange(sender, e);
        }



        void dataWindowControl_AfterSelector(object sender, AfterSelectorEventArgs e)
        {
            CustDataGridViewFuzzyQueryBoxColumn dataWindowColumn = (CustDataGridViewFuzzyQueryBoxColumn)OwningColumn;
            dataWindowColumn.OnAfterSelector(sender, e);
        }



        [Browsable(false)]
        public override Type EditType
        {
            get
            {
                return typeof(CustDataGridViewFuzzyQueryBoxEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(string);
            }
        }
        //private CustDataGridViewFuzzyQueryBoxEditingControl EditingDataWindow
        //{
        //    get
        //    {
        //        CustDataGridViewFuzzyQueryBoxEditingControl dataWindowControl = DataGridView.EditingControl as CustDataGridViewFuzzyQueryBoxEditingControl;

        //        return dataWindowControl;
        //    }
        //}
    }
}
